local juezuo = fk.CreateSkill{
  name = "lz__juezuo",
  tags = { "Phase" }
}

Fk:loadTranslationTable{
  ["lz__juezuo"] = "绝佐",
  [":lz__juezuo"] = "阶段技，你可以观看一名其他角色的手牌并可交给其至多两张牌，然后若其手牌中梅花/红桃/方片/黑桃牌数最多，其视为使用【杀】/【桃】/【洞烛先机】/【逐近弃远】。",
  ["#lz__juezuo"] = "绝佐：选择一名角色观看其手牌",
  ["#lz__juezuo-give"] = "绝佐：你可以交给 %src 至多两张牌",
  ["#lz__juezuo-use"] = "绝佐：请选择视为使用的牌",
}

juezuo:addEffect("active", {
  anim_type = "support",
  prompt = "#lz__juezuo",
  max_phase_use_time = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= player
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local to = effect.tos[1]
    if not to or to.dead then return end
    if not to:isKongcheng()then
      room:viewCards(player, {
        cards = to:getCardIds("h"),
        skill_name = juezuo.name,
      })
    end
    local to_give = room:askToCards(player, {
      min_num = 1,
      max_num = 2,
      include_equip = true,
      skill_name = juezuo.name,
      cancelable = true,
      prompt = "#lz__juezuo-give:"..to.id,
    })
    -- 交牌
    if to_give and #to_give > 0 then
      room:moveCardTo(to_give, Card.PlayerHand, to, fk.ReasonGive, juezuo.name, nil, true, player)
    end
    if to.dead then return end
    -- 分析花色数量
    local maxNum, numMap, suits = 0, {}, {}
    for _, id in ipairs(to:getCardIds("h")) do
      local suit = Fk:getCardById(id).suit
      if suit ~= Card.NoSuit then
        numMap[suit] = (numMap[suit] or 0) + 1
      end
    end
    for suit, num in pairs(numMap) do
      maxNum = math.max(maxNum, num)
    end
    if maxNum == 0 then return end
    for suit, num in pairs(numMap) do
      if num == maxNum then
        table.insert(suits, suit)
      end
    end
    -- 映射花色到牌名
    local nameMap = {
      [Card.Club] = "lvshi__slash",
      [Card.Heart] = "lvshi__peach",
      [Card.Diamond] = "foresight",
      [Card.Spade] = "chasing_near",
    }
    local names = table.map(suits, function(s) return nameMap[s] end)
    -- 处理视为使用牌
    while #names > 0 and not to.dead do
      local use = room:askToUseVirtualCard(to, {
        name = names,
        skill_name = juezuo.name,
        prompt = "#lz__juezuo-use",
        cancelable = true,
      })
      if use then
        local card_name = use.card.name
        table.removeOne(names, card_name)
      else
        break
      end
    end
  end,
})

return juezuo